package com.xueyi.workflow.controller;

import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.security.auth.pool.WorkflowPool;
import com.xueyi.workflow.domain.dto.CompleteTaskDto;
import com.xueyi.workflow.domain.dto.StartProcessDto;
import com.xueyi.workflow.domain.vo.TaskVo;
import com.xueyi.workflow.service.ProcessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "流程实例与任务管理")
@RestController
@RequestMapping("/process")
public class ProcessController {

    @Autowired
    private ProcessService processService;

    /** 部署流程定义 */
    @Operation(summary = "部署流程定义", description = "上传BPMN文件并部署流程定义")
    @PreAuthorize("@ss.hasAuthority('" + WorkflowPool.WORKFLOW_PROCESS_DEPLOY + "')")
    @PostMapping("/deploy")
    public AjaxResult deployProcess(@Parameter(description = "BPMN文件", required = true) @RequestParam("file") MultipartFile file) {
        return processService.deployProcess(file);
    }

    /** 启动流程实例 */
    @Operation(summary = "启动流程实例", description = "根据流程定义key和业务参数启动流程")
    @PreAuthorize("@ss.hasAuthority('" + WorkflowPool.WORKFLOW_PROCESS_START + "')")
    @PostMapping("/start")
    public AjaxResult startProcess(@Validated @RequestBody StartProcessDto dto) {
        return processService.startProcess(dto);
    }

    /** 查询待办任务 */
    @Operation(summary = "查询待办任务", description = "根据assignee查询待办任务列表")
    @PreAuthorize("@ss.hasAuthority('" + WorkflowPool.WORKFLOW_PROCESS_TASK_LIST + "')")
    @GetMapping("/tasks")
    public AjaxResult getTasks(@Parameter(description = "任务处理人", required = true) @RequestParam String assignee) {
        return processService.getTasks(assignee);
    }

    /** 分页查询待办任务 */
    @Operation(summary = "分页查询待办任务", description = "根据assignee分页查询待办任务")
    @PreAuthorize("@ss.hasAuthority('" + WorkflowPool.WORKFLOW_PROCESS_TASK_LIST + "')")
    @GetMapping("/tasks/page")
    public AjaxResult getTasksPage(@Parameter(description = "任务处理人", required = true) @RequestParam String assignee,
                                   @Parameter(description = "页码", required = false) @RequestParam(defaultValue = "1") int pageNum,
                                   @Parameter(description = "每页数量", required = false) @RequestParam(defaultValue = "10") int pageSize) {
        // TODO: 可集成PageHelper等分页插件
        return processService.getTasks(assignee); // 简单返回全部，后续可分页
    }

    /** 完成任务 */
    @Operation(summary = "完成任务", description = "根据任务ID和参数完成任务")
    @PreAuthorize("@ss.hasAuthority('" + WorkflowPool.WORKFLOW_PROCESS_TASK_COMPLETE + "')")
    @PostMapping("/complete")
    public AjaxResult completeTask(@Validated @RequestBody CompleteTaskDto dto) {
        return processService.completeTask(dto);
    }

    /** 查询流程实例历史 */
    @Operation(summary = "查询流程实例历史", description = "根据流程实例ID查询历史记录")
    @PreAuthorize("@ss.hasAuthority('" + WorkflowPool.WORKFLOW_PROCESS_HISTORY + "')")
    @GetMapping("/history")
    public AjaxResult getProcessHistory(@Parameter(description = "流程实例ID", required = true) @RequestParam String processInstanceId) {
        return processService.getProcessHistory(processInstanceId);
    }
}
